package com.itheima.book.manager.controller;

import com.itheima.book.manager.domain.Book;
import com.itheima.book.manager.service.BookService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;



public abstract class BaseBookController {

    public void start(){
        while (true){
            printView();
            useSelect();
        }
    }
      //----->方法一
//    public void printView(){}
    //----->方法二
      public abstract void printView();

    public void useSelect(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的选择");
        String next = scanner.next();

        switch (next){
            case "1":
                //TODO 添加图书
                inputBookInfo();
                break;
            case "2":
                //TODO 删除图书
                deleteByBookId();
                break;
            case "3":
                //TODO 修改图书
                updateByBookId();
                break;
            case "4":
                //TODO 查询图书
                findAll();
                break;
            case "5":
                //TODO 退出
                System.out.println("谢谢使用");
                return;
            default:
                System.out.println("请重新输入");
                break;
        }
    }

    //放到类中方法外成为成员变量？要加private修饰(这是一种习惯)，加了private？不写get/set方法(因为目前用不到)
    private BookService bookService = new BookService();
    public void inputBookInfo(){
        //?数组加了static修饰，多次调用方法创建bookservice对象就无所谓了吗？(是这样)
//        BookService bookService = new BookService();
        Scanner scanner = new Scanner(System.in);
        String bookId;
        while (true){
            System.out.println("请输入bookId");
            bookId = scanner.next();
//        BookService bookService = new BookService();
            boolean flag = bookService.idExistsBookId(bookId);
            if (flag){
                System.out.println("图书Id已存在，请重新输入");
            } else {
                break;
            }
        }

        System.out.println("请输入图书名称");
        String bookName = scanner.next();
        System.out.println("请输入图书出版日期");
        String publishDate = scanner.next();
        Date parsePublishDate = null;
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
            parsePublishDate = simpleDateFormat.parse(publishDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println("请输入图书价格");
        String price = scanner.next();
        double doublePrice = Double.parseDouble(price);

        Book book = new Book();
        book.setBookID(bookId);
        book.setBookName(bookName);
        book.setPublishDate(parsePublishDate);
        book.setPrice(doublePrice);

//        BookService bookService = new BookService();
        boolean flag = bookService.addBook(book);
        if (flag){
            System.out.println("添加图书成功");
        } else {
            System.out.println("添加图书失败");
        }

    }

    public void findAll(){
        System.out.println("图书Id\t\t图书名称\t\t图书出版日期\t\t图书价格");
        Book[] allBooks = bookService.findAll();
        for (int i = 0; i < allBooks.length; i++) {
            Book book = allBooks[i];
            if (book != null){

                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd");
                String publishDate = simpleDateFormat.format(book.getPublishDate());
                System.out.println(book.getBookID() + "\t\t" + book.getBookName() + "\t\t" +
                                    publishDate + "\t\t" + book.getPrice());
            }
        }
    }

    public void deleteByBookId(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要删除的图书Id");
        String bookId = scanner.next();
        boolean flag = bookService.idExistsBookId(bookId);
        while (true){
            if (flag){
                bookService.deleteByBookId(bookId);
                System.out.println("删除图书成功");
                break;
            } else {
                System.out.println("Id不存在，请重新输入");
            }
        }

    }

    public void updateByBookId(){
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.println("请输入你要修改的图书Id");
            String bookId = scanner.next();
            boolean flag = bookService.idExistsBookId(bookId);
            if (flag){
                System.out.println("请输入图书名称");
                String bookName = scanner.next();
                System.out.println("请输入图书出版日期");
                String publishDate = scanner.next();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
                Date parsePublishDate = null;
                try {
                    parsePublishDate = simpleDateFormat.parse(publishDate);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                System.out.println("请输入图书价格");
                String price = scanner.next();
                double doublePrice = Double.parseDouble(price);

                Book book = new Book();
                book.setBookID(bookId);
                book.setBookName(bookName);
                book.setPublishDate(parsePublishDate);
                book.setPrice(doublePrice);

                bookService.updateBookByBookId(book);
                System.out.println("修改图书成功");
                break;
            } else {
                System.out.println("你输入的图书Id不存在，请重新输入");
            }
        }

    }
}
